package com.lc.entrance;

import com.lc.access.XChildMapper;
import com.lc.access.XTestMapper;
import com.lc.model.XChild;
import com.lc.model.XTest;
import com.lc.vos.XTestVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @author: liuc
 * @date: 2020/4/23
 * @desc:
 */
public class MybatisFirst {
    public static void main(String[] args) {
        String resource = "my-mybatis.xml";
        Reader reader;
        try {
            reader = Resources.getResourceAsReader(resource);
            SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
            SqlSession session = sqlMapper.openSession();
            try {
                //testBaseSelect(session);
                //testAssociation(session);
                //testTypeHandler(session);
                //testCollection(session);
                testGetObjColPrefix(session);
            } finally {
                session.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testBaseSelect(SqlSession session) {
        //传 '1xxxx'会被当成1，数据库中该字段的类型是int
        //XTest xTesta = session.selectOne("com.lc.access.XTestMapper.getObj", "1 asdasd1");
        //XTest xTest2 = session.selectOne("com.lc.access.XTestMapper.checkObj", "2");
        //此处的mapper文件就只是一个单独的mapper文件,不对应某个接口
        //XTest xTest1 = session.selectOne("com.lc.access.XTestMapperx.getObj", "1");
        XTestMapper mapper = session.getMapper(XTestMapper.class);
        XTest xTest_1 = mapper.getObj("1");
        System.out.println(xTest_1);
    }

    public static void testTypeHandler(SqlSession session) {
        XChildMapper mapper = session.getMapper(XChildMapper.class);
        XChild xChild = mapper.getObj("1");
        System.out.println(xChild);
    }

    public static void testCollection(SqlSession session) {
        XTestMapper mapper = session.getMapper(XTestMapper.class);
        XTestVo xTest_1 = mapper.getObjCollection("1");
        System.out.println(xTest_1);
    }

    public static void testAssociation(SqlSession session) {
        XTestMapper mapper = session.getMapper(XTestMapper.class);
        XTest xTest_1 = mapper.getObjAssociation("1");
        System.out.println(xTest_1);
    }

    public static void testGetObjColPrefix(SqlSession session) {
        XTestMapper mapper = session.getMapper(XTestMapper.class);
        XTestVo xTest_1 = mapper.getObjColPrefix("1");
        System.out.println(xTest_1);
    }
}
